package com.example.victory.utils;

import com.cgws.fintech.sdk.common.model.FTBean;
import com.cgws.fintech.sdk.common.model.JumpUrl;
import com.cgws.fintech.sdk.common.model.NativeBean;
import com.cgws.fintech.sdk.common.model.RNBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 校验辅助类
 */
public class ValidationHelper {
    private static final Logger logger = LoggerFactory.getLogger(ValidationHelper.class);

    /**
     * 验证邮箱
     *
     * @param str 待验证的字符串
     * @return 如果是符合的字符串, 返回 <b>true </b>,否则为 <b>false </b>
     */
    public static boolean isEmail(String str) {
        String regex = "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        return match(regex, str);
    }

    /**
     * 验证网址Url
     *
     * @param str 待验证的字符串
     * @return 如果是符合格式的字符串, 返回 <b>true </b>,否则为 <b>false </b>
     */
    public static boolean isUrl(String str) {
        String regex = "^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://|[fF][tT][pP]://)(?:([A-Za-z]+):)?(\\/{0,3})([0-9.\\-A-Za-z]+)(?::(\\d+))?(?:\\/([^?#]*))?(?:\\?([^#]*))?(?:#(.*))?$";
        return match(regex, str);
    }

    /**
     * 是否为跳转链接
     *
     * @param str 待验证的字符串
     * @return 是否匹配
     */
    public static boolean isJumpUrl(String str) {
        // {"type":"native","nativeBean":{"identity":"newsList"}}
        // {"type":"h5", "h5Bean":{"title":"公告","url":"https://stgfintech.cgws.com:8443/h5/information_inside-page/html/information_inside.html?id=1053"}}
        // {"type":"ft", "ftBean":{"identity": "finoteam","pagePath":""}}
        try {
            JumpUrl jumpUrl = JsonHelper.fromJson(str, JumpUrl.class);
            switch (jumpUrl.getType()) {
                case "h5":
                    return jumpUrl.getH5Bean() != null && isUrl(jumpUrl.getH5Bean().getUrl());
                case "rn":
                    RNBean rnBean = jumpUrl.getRnBean();
                    return rnBean != null && rnBean.getBundleId() != null && rnBean.getDisplayName() != null && isUrl(rnBean.getIconUrl());
                case "native":
                    NativeBean nativeBean = jumpUrl.getNativeBean();
                    return nativeBean != null && StringUtils.isNotBlank(nativeBean.getIdentity());
                case "ft":
                    FTBean ftBean = jumpUrl.getFtBean();
                    return ftBean != null && StringUtils.isNotBlank(ftBean.getIdentity());
                default:
                    return false;
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            return false;
        }
    }

    /**
     * 验证电话号码
     *
     * @param str 待验证的字符串
     * @return 如果是符合格式的字符串, 返回 <b>true </b>,否则为 <b>false </b>
     */
    public static boolean isTelephone(String str) {
        String regex = "^(\\d{3,4}-)?\\d{6,8}$";
        return match(regex, str);
    }

    /**
     * 验证输入手机号码
     *
     * @param str 待验证的字符串
     * @return 如果是符合格式的字符串, 返回 <b>true </b>,否则为 <b>false </b>
     */
    public static boolean isMobileNo(String str) {
        String regex = "^1\\d{10}$";
        return match(regex, str);
    }

    /**
     * 验证输入密码条件(字符与数据同时出现)
     *
     * @param str 待验证的字符串
     * @return 如果是符合格式的字符串, 返回 <b>true </b>,否则为 <b>false </b>
     */
    public static boolean isPassword(String str) {
        String regex = "[A-Za-z]+[0-9]";
        return match(regex, str);
    }

    /**
     * 验证输入密码长度 (6-18位)
     *
     * @param str 待验证的字符串
     * @return 如果是符合格式的字符串, 返回 <b>true </b>,否则为 <b>false </b>
     */
    public static boolean isPasswordLength(String str) {
        String regex = "^\\d{6,18}$";
        return match(regex, str);
    }

    /**
     * _#
     * 验证输入身份证号
     *
     * @param str 待验证的字符串
     * @return 如果是符合格式的字符串, 返回 <b>true </b>,否则为 <b>false </b>
     */
    public static boolean isIDcard(String str) {
        String regex = "(^\\d{18}$)|(^\\d{15}$)";
        return match(regex, str);
    }

    /**
     * 验证验证输入汉字
     *
     * @param str 待验证的字符串
     * @return 如果是符合格式的字符串, 返回 <b>true </b>,否则为 <b>false </b>
     */
    public static boolean isChinese(String str) {
        String regex = "^[\u4e00-\u9fa5],{0,}$";
        return match(regex, str);
    }

    /**
     * 校验是否为名称，只允许输入英文字母、数字、中文
     *
     * @param str 待验证的字符串
     * @param min 最小长度
     * @param max 最大长度
     * @return 是否匹配
     */
    public static boolean isName(String str, Integer min, Integer max) {
        String regex = "^[a-z0-9A-Z\u4e00-\u9fa5]{" + min + "," + max + "}$";
        return match(regex, str);
    }

    /**
     * 是否整数
     *
     * @param value 待验证的字符串
     * @return 是否匹配
     */
    public static boolean isInt(String value) {
        String regex = "^-?[1-9]\\d*$";
        return match(regex, value);
    }

    /**
     * @param regex 正则表达式字符串
     * @param str   要匹配的字符串
     * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
     */
    private static boolean match(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    public static void main(String[] args) {
        boolean result = ValidationHelper.isUrl("http1://112");
        result = ValidationHelper.isInt("a");
        result = ValidationHelper.isInt("122222");
        result = ValidationHelper.isInt("-122222");
        result = ValidationHelper.isUrl("https:/112");
        result = ValidationHelper.isUrl("http:/www.baidu.com");
        result = ValidationHelper.isUrl("https://112");
        result = ValidationHelper.isUrl("http://www.baidu.com");
        result = ValidationHelper.isUrl("Https://www.baidu.com");
        result = ValidationHelper.isUrl("https://devfintech.cgws.com:8443/api/platform-resource/ops/2018-10/1540799718993-adbba3bdd38d4a53a2952b2f7261a8d5.json");
        result = ValidationHelper.isTelephone("0755-26783333");
        result = ValidationHelper.isTelephone("1341846777");
        result = ValidationHelper.isMobileNo("134184677755");
        result = ValidationHelper.isMobileNo("+86 13051492088");
        result = ValidationHelper.isMobileNo("18129988888");
        result = ValidationHelper.isMobileNo("1812998888");
        result = ValidationHelper.isEmail("1@2.com");
        result = ValidationHelper.isEmail("1@2");
        result = ValidationHelper.isEmail("a");
        result = ValidationHelper.isName("a2我b4的", 0, 20);
        result = ValidationHelper.isName("a2我b4的", 1, 5);
        result = ValidationHelper.isName("a2我b4的", 5, 5);
        result = ValidationHelper.isName("a2我b4的", 6, 11);
        result = ValidationHelper.isName("a2我b4的", 7, 11);
        result = ValidationHelper.isName("a2我b_4的", 0, 0);
        result = ValidationHelper.isName("a2我b_4的*", 0, 11);
        result = ValidationHelper.isUrl("https://k.cgws.com/pic-king_h5/#/home?query=1&a=2");
        result = ValidationHelper.isUrl("https://www.baidu.com/s/a-bb~c_d/c_d#/a?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=maven%20%E6%8F%92%E4%BB%B6%20%E7%BC%96%E5%86%99%20intellij&oq=maven%2520%25E6%258F%2592%25E4%25BB%25B6%2520%25E7%25BC%2596%25E5%2586%2599&rsv_pq=8fe14f2d000382ab&rsv_t=26c0beIFftP3j7ypG6lgdgOxxq1G4D1T%2FOrhF%2FVGQhZCJ3Nu1Wp6cfjDPOo&rqlang=cn&rsv_enter=0&inputT=2561&rsv_sug3=101&rsv_sug1=57&rsv_sug7=000&rsv_sug2=0&rsv_sug4=3622&rsv_sug=1");
        result = ValidationHelper.isUrl("htt://k.cgws.com/pic-king_h5/#/home?query=1&a=2");
        result = ValidationHelper.isJumpUrl("{\"type\":\"h5\", \"h5Bean\":{\"title\":\"公告\",\"url\":\"https:{{stgfintech.cgws.com:8443/h5/information_inside-page/html/information_inside.html?id=1053\"}}");
        result = ValidationHelper.isJumpUrl("{\"type\":\"h5\",\"rnBean\":{\"bundleId\":\"ttt\",\"displayName\":\"ttt\",\"iconUrl\":\"https://devfintech.cgws.com:8443/api/platform-resource/ops/2018-11/1541726678403-01a4362250c14498aabc24be87839817.png\"}}");

        result = ValidationHelper.isUrl("http://61.144.241.205:9080/likekline/src/app/chart.html?code=000068&market=33&name=%E5%90%8C%E8%8A%B1%E9%A1%BA");

        if (result) {

        }
    }
}
